package com.ephics.ui
{
	import com.ephics.events.ScrollEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import spark.primitives.Rect;

	public class ScrollBar extends Sprite
	{
		protected var scrollHeight:int = 400;
		protected var scrollableArea:int = 298;
		
		protected var scrollBg:Sprite;
		protected var scrollBtn:Sprite;

		private var _percent:Number;
		
		public function ScrollBar()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		public function get percent():Number
		{
			return _percent;
		}

		public function set percent(value:Number):void
		{
			if(value == percent)
				return;
			
			_percent = value;
			dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL_UPDATE, percent));
		}

		private function onAddedToStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			createScrollBackground();
			createScrollButton();
			enable();
		}
		
		private function createScrollBackground():void
		{
			scrollBg = new Sprite();
			scrollBg.graphics.beginFill(0);
			scrollBg.graphics.drawRect(0,0, 10, scrollHeight);
			scrollBg.graphics.endFill();
			addChild(scrollBg);
		}
		
		private function createScrollButton():void
		{
			scrollBtn = new Sprite();
			scrollBtn.graphics.beginFill(0xff0066);
			scrollBtn.graphics.drawRect(1,1, 8, 100);
			scrollBtn.graphics.endFill();
			addChild(scrollBtn);
		}
		
		public function enable():void
		{
			scrollBtn.buttonMode = true;
			scrollBtn.mouseChildren = false;
			scrollBtn.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			addEventListener(Event.ENTER_FRAME, updateStatus);
		}
		
		public function disable():void
		{
			scrollBtn.buttonMode = false;
			scrollBtn.mouseChildren = false;
			scrollBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			removeEventListener(Event.ENTER_FRAME, updateStatus);
		}
		
		protected function onMouseDown(event:MouseEvent):void
		{
			scrollBtn.startDrag(false, new Rectangle(0,0,0, scrollableArea));
			stage.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			stopDrag();
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		protected function onMouseWheel(event:MouseEvent):void
		{
			var targetY:Number = scrollBtn.y - event.delta;
			if(targetY < 0)
				targetY = 0;
			
			if(targetY > scrollableArea)
				targetY = scrollableArea;
			
			scrollBtn.y = targetY;
		}
		
		protected function updateStatus(event:Event):void
		{
			percent = Math.floor(scrollBtn.y * 100 / scrollableArea);
		}
		
		public function unload():void
		{
			disable();
			while(numChildren)
			{
				removeChildAt(0);
			}
			if(this.parent)
				this.parent.removeChild(this);
		}
	}
}